//
//  HomePageVC.m
//  jzyStudents
//
//  Created by 任辰东 on 2022/9/22.
//

#import "HomePageVC.h"
#import "SDCycleScrollView.h"
#import "UserInfoView.h"
#import "LoginVC.h"
#import "HomePageTitleView.h"
#import "RCDGroupBtnsView.h"
#import "HomePageApis.h"
#import "HomePageModel.h"
#import "ReportCardListVC.h"
//#import "XueQingFenXiVC.h"
//#import "BookOwnerPageVC.h"
//#import "HomeworkPageVC.h"
//#import "ClassExerciseListVC.h"
//#import "ExaminationListVC.h"
#import "ScanLoginVC.h"

#import "StudyListVC.h"
#import "XinLiCePingListVC.h"

@interface HomePageVC ()

@property (nonatomic, strong) UIView *notifyView;
@property (nonatomic, strong) SDCycleScrollView *notifyScrollView;
@property (nonatomic, strong) UIImageView *notifyIV;

@property (nonatomic, strong) UserInfoView *infoView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) HomePageTitleView *titleView;
@property (nonatomic, strong) RCDGroupBtnsView *groupBtnsView;
@property (nonatomic, copy) NSArray <HomePageModel *>*dataArray;
@end

@implementation HomePageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"教之源学生";
    
//    [self setNavRightItemWithImage:UIImageWithName(@"scan") highlightImg:UIImageWithName(@"scan")];
    
//    [self getData];
    
//    MJRefreshStateHeader *header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
//    [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
//    [header setTitle:@"松开刷新" forState:MJRefreshStatePulling];
//    [header setTitle:@"正在加载中..." forState:MJRefreshStateRefreshing];
//    header.lastUpdatedTimeLabel.hidden = YES;
//    self.bgScrollView.mj_header = header;
    
    WeakSelf
    [self.infoView setLoginBtnClicked:^{
       StrongSelf
        DLog(@"登录");
        LoginVC *loginVC = [LoginVC new];
        [self.navigationController pushViewController:loginVC animated:YES];
    }];
    
    [self.groupBtnsView setDidSelectedIndex:^(NSInteger index) {
           
        StrongSelf
        
        switch (index) {
            case 0:
            {
                DLog(@"学业报告");
                ReportCardListVC *reportVC = [ReportCardListVC new];
                [self.navigationController pushViewController:reportVC animated:YES];
            }
                break;
            case 1:
            {
                DLog(@"心理测评");
                XinLiCePingListVC *cepingVC = [XinLiCePingListVC new];
                [self.navigationController pushViewController:cepingVC animated:YES];
            }
                break;
            case 2:
            {
                DLog(@"学习中心");
                StudyListVC *studytVC = [StudyListVC new];
                [self.navigationController pushViewController:studytVC animated:YES];
            }
                break;
                
            default:
                break;
        }
    }];
    
}

-(void)RightBarItemClick:(UIButton *)sender
{
    ScanLoginVC *scanVC = [ScanLoginVC new];
    RCDBaseNavigationController *nav = [[RCDBaseNavigationController alloc]initWithRootViewController:scanVC];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:NULL];
}
 
- (void)getData
{
    WeakSelf
    [self showLoadingHUDWithMessage:nil];
    [HomePageApis ctb_zj_ShouyeRequestWithParams:@{} SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        DLog(@"首页 === %@",responseData);
        [self.bgScrollView.mj_header endRefreshing];
        NSMutableArray *groupArr = [NSMutableArray array];
        NSArray *data = [HomePageModel mj_objectArrayWithKeyValuesArray:responseData];
        self.dataArray = [NSArray arrayWithArray:data];
        for (HomePageModel *model in data) {
            NSDictionary *dic = @{
                                  @"image":[NSString stringWithFormat:@"%@",model.yingyong_img],
                                  @"title":model.yingyong_name
                                  };
            [groupArr addObject:dic];
        }
        [self.groupBtnsView configWithTitleAndImgsArr:groupArr column:5 isAddLine:NO];
            
    } FailureBlock:^(NSNumber *code, NSString *msg) {
        StrongSelf
        [self.bgScrollView.mj_header endRefreshing];
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
}

-(void)setupViews
{
//    self.view.backgroundColor = UIColorFromHex(kColorWhite);
//    [self.bgScrollView.contentView addSubview:self.infoView];
    [self.bgScrollView.contentView addSubview:self.lineView];
    
    [self.bgScrollView.contentView addSubview:self.notifyView];
    [self.notifyView addSubview:self.notifyScrollView];
    [self.notifyView addSubview:self.notifyIV];
    
    [self.bgScrollView.contentView addSubview:self.bottomView];
    [self.bottomView addSubview:self.titleView];
    [self.bottomView addSubview:self.groupBtnsView];
    
}

-(void)setupLayout
{
//    [self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
//       
//        make.top.left.right.equalTo(self.bgScrollView.contentView);
//    }];
//    

    
    [self.notifyView mas_makeConstraints:^(MASConstraintMaker *make) {
        
//        make.top.equalTo(self.scrollView.mas_bottom);
//        make.left.right.equalTo(self.bgScrollView.contentView);
        make.top.left.right.equalTo(self.bgScrollView.contentView);
        make.height.mas_equalTo(45);
    }];
    
    [self.notifyIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.notifyView).offset(15);
        make.left.equalTo(self.notifyView).offset(15);
        make.height.mas_equalTo(14);
        make.width.mas_equalTo(16);
    }];
    
    [self.notifyScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.notifyIV.mas_right);
        make.top.bottom.equalTo(self.notifyView);
        make.right.equalTo(self.notifyView).offset(-15);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.notifyView.mas_bottom).offset(0);
        make.left.right.equalTo(self.bgScrollView.contentView);
        make.height.mas_equalTo(10);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.top.equalTo(self.lineView.mas_bottom).offset(0);
        make.left.right.equalTo(self.bgScrollView.contentView);
        make.bottom.equalTo(self.bgScrollView.contentView).offset(0);
    }];
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.bottomView);
        make.left.right.equalTo(self.bottomView);
        make.height.mas_equalTo(50);
    }];
    
    [self.groupBtnsView mas_makeConstraints:^(MASConstraintMaker *make) {
          
        make.top.equalTo(self.titleView.mas_bottom);
        make.left.right.equalTo(self.bottomView);
        make.bottom.equalTo(self.bottomView);
    }];
}

#pragma mark -- lazylaod

-(UserInfoView *)infoView
{
    if (!_infoView) {
        _infoView = [UserInfoView new];
        _infoView.backgroundColor = UIColorFromHex(kColorWhite);
        [_infoView addBottomLine];
    }
    return _infoView;
}

-(UIView *)lineView
{
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = UIColorFromHex(kColorBG);
    }
    return _lineView;
}

-(UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [UIView new];
        _bottomView.backgroundColor = UIColorFromHex(kColorWhite);
        [_bottomView addTopLine];
    }
    return _bottomView;
}

-(HomePageTitleView *)titleView
{
    if (!_titleView) {
        _titleView = [[HomePageTitleView alloc]initWithTitle:@"我的应用"];
        [_titleView addBottomLineWithEdge:UIEdgeInsetsMake(0, 15, 0, 15)];
    }
    return _titleView;
}

-(RCDGroupBtnsView *)groupBtnsView
{
    if (!_groupBtnsView) {
        NSArray *dataArray = @[@{@"image":@"home_report",
                                 @"title":@"成绩报告"},
                               @{@"image":@"home_test",
                                @"title":@"心理测评"},
                               @{@"image":@"home_study",
                                @"title":@"学习中心"},
                               @{@"image":@"  ",
                                 @"title":@"  "},
                               @{@"image":@"  ",
                                 @"title":@"  "}];
        _groupBtnsView = [[RCDGroupBtnsView alloc]initWithTitleAndImgsArr:dataArray column:5 isAddLine:NO];
    }
    return _groupBtnsView;
}

- (UIView *)notifyView
{
    if (!_notifyView) {
        self.notifyView = [[UIView alloc] init];
        _notifyView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _notifyView;
}

-(UIImageView *)notifyIV
{
    if (!_notifyIV) {
        _notifyIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"notify"]];
    }
    return _notifyIV;
}

- (SDCycleScrollView *)notifyScrollView
{
    if (!_notifyScrollView) {
        self.notifyScrollView = [[SDCycleScrollView alloc] init];
        _notifyScrollView.scrollDirection = UICollectionViewScrollDirectionVertical;
        _notifyScrollView.onlyDisplayText = YES;
        _notifyScrollView.titlesGroup = @[[NSString stringWithFormat:@"欢迎%@登录教之源学生", [GVUserDefaults standardUserDefaults].student_name]];
        _notifyScrollView.titleLabelBackgroundColor = UIColorFromHex(kColorWhite);
        _notifyScrollView.titleLabelTextFont = UIFontSize(15);
        _notifyScrollView.titleLabelTextColor = UIColorFromHex(kColorTitle);
        _notifyScrollView.autoScrollTimeInterval = 3;
        _notifyScrollView.autoScroll = YES;
        _notifyScrollView.titleLabelTextAlignment = NSTextAlignmentLeft;
        [_notifyScrollView disableScrollGesture];
    }
    return _notifyScrollView;
}


@end
